package sz.com.postoffice.jxyzwgcase.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;
import sz.com.postoffice.jxyzwgcase.pojo.OperationTargetReturn;
import sz.com.postoffice.jxyzwgcase.pojo.OperationtargetInfo;
import sz.com.postoffice.jxyzwgcase.pojo.StatParam;
import sz.com.postoffice.jxyzwgcase.pojo.StatResult;
import sz.com.postoffice.jxyzwgcase.service.OperationTargetService;
import sz.com.postoffice.jxyzwgcase.service.StatService;
import sz.com.postoffice.jxyzwgcase.utils.DateStrUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(value = "运营统计信息", tags = "{运营统计信息API}")
@RestController
@Slf4j
@RequestMapping(value = "/stat")
public class StatController {
    @Resource
    private StatService statService;
    @Resource
    private OperationTargetService operationTargetService;

    @ApiOperation(value = "根据城市和时间统计运营数据", notes = "根据城市和时间统计运营数据")
    @RequestMapping(value = "/cities", method = RequestMethod.POST)
    public List<StatResult> statByCity(@RequestBody StatParam statParam) {
        System.out.println("开始查询...");
        String startTime = statParam.getStartDate() + " 00:00:00";
        String endTime = statParam.getEndDate() + " 23:59:59";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = DateStrUtil.getDateByStr(statParam.getStartDate(), dateFormat);
        Date endDate = DateStrUtil.getDateByStr(statParam.getEndDate(), dateFormat);
        long days = Math.abs((endDate.getTime() - startDate.getTime()) / 86400000) + 1;
        return statService.getStatResultByCity(statParam.getCityCodes(), startTime, endTime, (int) days);
    }

    @ApiOperation(value = "部门code和所选日期范围统计运营数据", notes = "部门code和所选日期范围统计运营数据")
    @RequestMapping(value = "/cities2", method = RequestMethod.POST)
    public List<OperationTargetReturn> statByCitys(@RequestBody StatParam statParam) {
        log.info("开始查询...OperationTarget");
        return operationTargetService.getOperationTargetByCity(statParam);
    }

    @ApiOperation(value = "部门code和所选日期范围使用app模块类型，查询使用人详情", notes = "部门code和所选日期范围使用app模块类型，查询使用人详情")
    @RequestMapping(value = "/usageDetails", method = RequestMethod.POST)
    public List<OperationtargetInfo> usageDetails(@RequestBody StatParam statParam) {
        log.info("开始查询...usageDetails");
        return operationTargetService.usageDetails(statParam);
    }

    @ApiOperation(value = "根据城市和所选日期范围统计运营数据", notes = "根据城市和所选日期范围统计运营数据")
    @RequestMapping(value = "/cities2ExportExcel", method = RequestMethod.GET)
    public void statByCites(StatParam statParam, HttpServletResponse response) throws IOException {
        log.info("开始导出...OperationTarget");
        //设置相应头对象
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = "运营数据(一线（营业部经理_揽投员）使用情况" + statParam.getStartDate() + "至" + statParam.getEndDate() + ".xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        XSSFWorkbook wb = operationTargetService.getOperationTargetExportExcel(statParam,"cities2");
        OutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
        wb.close();
    }

    @ApiOperation(value = "根据城市和所选日期范围统计运营数据", notes = "根据城市和所选日期范围统计运营数据")
    @RequestMapping(value = "/usageDetailsExportExcel", method = RequestMethod.GET)
    public void usageDetails(StatParam statParam, HttpServletResponse response) throws IOException {
        log.info("开始导出...usageDetails");
        //设置相应头对象
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/x-download");
        String fileName = "APP使用使用情况导出" + statParam.getStartDate() + "至" + statParam.getEndDate() + ".xlsx";
        fileName = URLEncoder.encode(fileName, "UTF-8");
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        XSSFWorkbook wb = operationTargetService.getOperationTargetExportExcel(statParam,"usageDetails");
        OutputStream out = response.getOutputStream();
        wb.write(out);
        out.close();
        wb.close();
    }

    @RequestMapping(value = "/collectDataFromADO", method = RequestMethod.GET)
    public String collectDataFromADO(StatParam statParam) {
        if (!"87c37038-c28a-45ae-807f-6d003ba82582".equals(statParam.getFunctionName())) return "false";
        long l = System.currentTimeMillis();
        List<String> days = DateStrUtil.getDays(statParam.getStartDate(), statParam.getEndDate());
        String[] reStr = days.toArray(new String[days.size()]);
        operationTargetService.collectDataFromADO(reStr);
        return String.valueOf(System.currentTimeMillis() - l);
    }
}
